package com.salmon.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 可以将此类注入到spring容器中，采用spring boot 启动时进行启动
 */
public class WSServer {

	private static final WSServer instance = new WSServer();
	
	public static WSServer getInstance() {
		return instance;
	}
	
	private EventLoopGroup mainGroup;
	private EventLoopGroup subGroup;
	private ServerBootstrap server;
	//private ChannelFuture future;
	
	public WSServer() {
		mainGroup = new NioEventLoopGroup();
		subGroup = new NioEventLoopGroup();
		server = new ServerBootstrap();
		server.group(mainGroup, subGroup)
			.channel(NioServerSocketChannel.class)
			.childHandler(new WSServerInitializer());
	}
	
	public void start() {
		//this.future = server.bind(8088);
		server.bind(8088);
		System.err.println("netty websocket server 启动完毕...[8088]");
	}

	public static void main(String[] args) {
		WSServer.getInstance().start();
	}
}
